package cn.zysuyuan.item.controller;

import cn.zysuyuan.common.entity.PageResult;
import cn.zysuyuan.common.entity.R;
import cn.zysuyuan.item.entity.Device;
import cn.zysuyuan.item.service.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.Response;
import java.util.HashMap;
import java.util.Map;

/**
 * @author flypig
 * @version 1.0
 * @date 2019/10/27 19:53
 */
@Api(tags = "设备管理")
@RestController
@RequestMapping("device")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    /**
     * 不使用Map<String,Object></> 的传参
     * @RequestParam(value = "key", required = false) String key,
     * @RequestParam(value = "page", defaultValue = "1") Integer page,
     * @RequestParam(value = "size", defaultValue = "10") Integer size
     * @return
     */
    @ApiOperation(value = "设备管理", notes = "列表查询")
    @GetMapping("devicedetail")
    public ResponseEntity<PageResult<Device>> queryDeviceByPage(@RequestParam  Map<String,Object> map){
        PageResult<Device> result = this.deviceService.queryDevicesByPage(map);
        if (CollectionUtils.isEmpty(result.getRows())){
            return ResponseEntity.notFound().build();
//            result = null;
//            return ResponseEntity.ok(result);
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "新增设备信息",notes = "新增设备信息")
    @PostMapping("devicedetail")
    public R save(@RequestBody Device device) {
        System.out.println("开始保存...");
        if(deviceService.insert(device) >  0){
            Map<String,Object> map = new HashMap<>();
            map.put("flag",true);
            return R.ok(map);
        }else{
            return R.error();
        }
    }

    @ApiOperation(value = "修改设备信息",notes = "修改设备信息")
    @PutMapping("devicedetail")
    public R update(@RequestBody Device device) {
        System.out.println("开始更新....");
        if (deviceService.update(device) > 0){
            Map<String,Object> map = new HashMap<>();
            map.put("flag",true);
            return R.ok(map);
        }else{
            return R.error();
        }
    }

    @ApiOperation(value = "删除设备信息",notes = "删除设备信息")
    @DeleteMapping("devicedetail")
    public R delete(@RequestParam("id") String id) {
        System.out.println("开始删除....");
        if (deviceService.delete(id) > 0){
            Map<String,Object> map = new HashMap<>();
            map.put("flag",true);
            return R.ok(map);
        }else{
            return R.error();
        }
    }


}
